package com.fromfuture.xuhuidoc.android.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TextView;

import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.adapter.MyYuYueViewPageAdapter;
import com.fromfuture.xuhuidoc.android.db.MyYuYueDb;
import com.fromfuture.xuhuidoc.android.fragments.MyYuYueFragment;

import java.util.ArrayList;
import java.util.List;

public class MyYuYueActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{

    private ViewPager viewPager;
    private static TextView xsyuyue=null,xxyuyue=null,title=null;
    private View my_yuyue_RadioGroup_view_01,my_yuyue_RadioGroup_view_02;
    private HorizontalScrollView hvChannel;
    private MyYuYueViewPageAdapter adapter=null;
    private List<Fragment> fragmentList=new ArrayList<Fragment>();
    private ImageView imageView_back;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_yu_yue);
        initView();
    }


    private void initView(){
        title= (TextView) findViewById(R.id.my_yuyue_title);
        xsyuyue=(TextView) super.findViewById(R.id.my_yuyue_RadioGroup_01);
        xxyuyue= (TextView) super.findViewById(R.id.my_yuyue_RadioGroup_02);
        imageView_back= (ImageView)super.findViewById(R.id.YuYueActivity_back);
        my_yuyue_RadioGroup_view_01=super.findViewById(R.id.my_yuyue_RadioGroup_view_01);
        my_yuyue_RadioGroup_view_02=super.findViewById(R.id.my_yuyue_RadioGroup_view_02);
        viewPager=(ViewPager)super.findViewById(R.id.my_yuyue_viewpage);
        viewPager.addOnPageChangeListener(this);

        type=getIntent().getStringExtra("type");
        title.setText(type);
        if (type.equals("我的预约")){
            xsyuyue.setText("线上预约（0）");
            xxyuyue.setText("线下预约（0）");
        }else if (type.equals("模板管理")){
            xsyuyue.setText("健康方案");
            xxyuyue.setText("注意事项");
        }

        tabview(0);
        xsyuyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
                tabview(0);
            }
        });
        xxyuyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
                tabview(1);
            }
        });
        imageView_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initViewPager(type);
    }

    private void initViewPager(String type){
        for (int i=0;i<MyYuYueDb.getMyYuYueDb().setSelectedChannel(type).getSelectedChannel().size();i++){
            MyYuYueFragment myYuYueFragment=new MyYuYueFragment();
            Bundle bundle=new Bundle();
            bundle.putString("weburl",MyYuYueDb.getMyYuYueDb().getSelectedChannel().get(i).getWeburl());
            bundle.putString("name",MyYuYueDb.getMyYuYueDb().getSelectedChannel().get(i).getName());
            myYuYueFragment.setArguments(bundle);
            fragmentList.add(myYuYueFragment);
        }
        adapter=new MyYuYueViewPageAdapter(super.getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(adapter);
    }

    /**
     * 顶部线上预约与线下预约的设置
     * */
    private void tabview(int index){
        switch (index){
            case 0:
                xsyuyue.setTextColor(Color.parseColor("#3BAFE7"));
                xxyuyue.setTextColor(Color.parseColor("#000000"));
                my_yuyue_RadioGroup_view_01.setBackgroundColor(Color.parseColor("#3BAFE7"));
                my_yuyue_RadioGroup_view_02.setBackgroundColor(Color.parseColor("#00000000"));
                break;
            case 1:
                xsyuyue.setTextColor(Color.parseColor("#000000"));
                xxyuyue.setTextColor(Color.parseColor("#3BAFE7"));
                my_yuyue_RadioGroup_view_01.setBackgroundColor(Color.parseColor("#00000000"));
                my_yuyue_RadioGroup_view_02.setBackgroundColor(Color.parseColor("#3BAFE7"));
                break;
        }
    }

    /**
     * 滑动ViewPager时调整顶部的位置以便显示按钮
     * @param idx
     */
    private void setTab(int idx){
        switch (idx){
            case 0:
                tabview(0);
                break;
            case 1:
                tabview(1);
                break;
        }
    }
    @Override
    public void onPageScrollStateChanged(int arg0) {
    }
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }
    @Override
    public void onPageSelected(int position) {
        setTab(position);
    }

    public static void XsyuyueNum(int num){
        xsyuyue.setText("线上预约（"+num+"）");
    }
    public static void XxyuyueNum(int num){
        xxyuyue.setText("线下预约（"+num+"）");
    }


}
